const { DB } = require('./sqlite3');

const TriggerLogDao = {
    // log : TriggerLog
    addOne(log, callback) {
        DB.run(`INSERT INTO trigger_log (s_id, p_id, t_price, t_time) VALUES ( ${log.s_id} , ${log.p_id} , ${log.t_price} , ${log.t_time || Date.now()} )`);
        typeof callback === "function" && callback();
    },
    addStack(stack, callback) {
        DB.run("INSERT INTO trigger_log (s_id, p_id, t_price, t_time) VALUES ",
            stack.map(e => `(${e.s_id}, ${e.p_id}, ${e.t_price}, ${e.t_time})`).join(',')
        );
        typeof callback === "function" && callback();
    },
    getBetween(start, end, callback) {
        db.all(`SELECT s_id, p_id, t_price, t_time FROM trigger_log WHERE t_time BETWEEN ${start} AND ${end} `, (err, rows) => {
            typeof callback === "function" && callback(rows);
        });
    },
    clearAll(callback) {
        DB.run('DELETE FROM trigger_log');
        typeof callback === "function" && callback();
    }
}

module.exports.TriggerLogDao = TriggerLogDao;